package club.babyf.springai.dao;

import club.babyf.springai.entity.AiMessageFetcher;
import club.babyf.springai.entity.AiSession;
import club.babyf.springai.entity.AiSessionFetcher;
import club.babyf.springai.entity.AiSessionTable;
import cn.dev33.satoken.stp.StpUtil;
import org.babyfish.jimmer.spring.repository.JRepository;

import java.util.List;

public interface AiSessionRepository extends JRepository<AiSession, String> {

    AiSessionTable t = AiSessionTable.$;

    AiSessionFetcher FETCHER = AiSessionFetcher.$.allScalarFields().messages(AiMessageFetcher.$.allScalarFields().sessionId());

    default List<AiSession> findByUser() {
        return sql().createQuery(t)
                .where(t.creatorId().eq(StpUtil.getLoginIdAsString()))
                .orderBy(t.createdTime().desc())
                .select(t.fetch(FETCHER))
                .execute();
    }

}
